<?php
namespace My\Controller;

use Utuo\BackgroundController;

class NavigateController extends BackgroundController {
	
	private $model = null;
	private $modelId;
	
	public function _initialize() {
		$this->model = I("model", "");
		$this->modelId = M("Model")->where("tag='%s'", $this->model)->getField("id");
		$this->setControllerTitle("一键导航管理");
		$this->setActionTitle(array(
				'index' => '一键导航设置',
		));
	}
	
	// 一键导航设置
	public function indexAction() {
		$m = M("Navigate");
		$navigate = $m->where("cid=%d and mid=%d", $this->aid, $this->modelId)->find();
		if(!$navigate) {
			false !== $m->add(array("cid"=>$this->aid, "mid" => $this->modelId)) or $this->error("添加一键导航配置失败");
			$navigate = $m->where("cid=%d and mid=%d", $this->aid, $this->modelId)->find();
		}
		$this->assign("navigate", $navigate);
		$this->assign("pointJSON",($navigate['lng'] && $navigate['lat'])?json_encode(array('lng'=>$navigate['lng'], 'lat'=>$navigate['lat'], 'adr'=>$navigate['address'])):"");
		$this->display();
	}
	
	// 一键导航设置提交
	public function indexPostAction() {
		$d = D('Navigate');
		$d->create() and $d->cid = $this->aid and false !== $d->save() and $this->ajaxSuccess("一键导航保存成功");
		$this->ajaxError();
	}
	
}